using UnityEngine;
using DG.Tweening;
using Manager;
using System.Collections;

public class FallenExperience : MonoBehaviour
{
    [SerializeField]
    float upOffet;//向上偏移量
    private Rigidbody rb;

    [SerializeField]
    private GameObject plane;
    private void OnEnable()
    {
        GameManager.Instance.OnEnemyNumIsZero += MoveToPlayer;

        plane = GameObject.Find("Plane");
        //计算地面的边界位置
        Vector3 min = plane.GetComponent<MeshRenderer>().bounds.min;
        Vector3 max = plane.GetComponent<MeshRenderer>().bounds.max;


        Vector3 pos = transform.position;
        //随机偏移
        float x = Random.Range(-.5f, .5f);
        float z = Random.Range(-.5f, .5f);
        transform.position = new Vector3(pos.x + x, pos.y + upOffet, pos.z + z);
        //当前位置不能超过地面边界
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, min.x + 1f, max.x - 1f),
            transform.position.y,
            Mathf.Clamp(transform.position.z, min.z + 1f, max.z - 1f));

        rb = GetComponent<Rigidbody>();

    }

    private void OnDisable()
    {
        GameManager.Instance.OnEnemyNumIsZero -= MoveToPlayer;

        StopAllCoroutines();
    }

    //触发器更大一点可以让玩家更好拾取
    private void OnTriggerEnter(Collider other)
    {

        if (other.gameObject.CompareTag("Ground"))
        {
            //把物体设置为静止状态，不在能移动
            rb.velocity = Vector3.zero;
            //rb.useGravity = false;
            rb.isKinematic = true;
        }
    }

    /*    private void Update()
        {
            //利用dotween 移动到玩家位置
            if (GameManager.Instance.enemysNum <= 0 && gameObject)
                transform.DOMove(Player.Player.instance.transform.position, 1f).SetEase(Ease.OutQuad);
        }*/

    /// <summary>
    /// 移动到玩家位置
    /// </summary>
    /*    public void MoveToPlayer()
        {
            //利用dotween 移动到玩家位置
            transform.DOMove(Player.Player.instance.transform.position, 1f).SetEase(Ease.OutQuad).onComplete = () =>
            {
                gameObject.transform.parent.gameObject.SetActive(false);
            };
        }*/

    public void MoveToPlayer()
    {
        StartCoroutine(FollowPlayerCoroutine());
    }

    /// <summary>
    /// 经验球在移动过程中持续追踪玩家的当前位置（不是固定目标点）
    /// </summary>
    /// <returns></returns>
    private IEnumerator FollowPlayerCoroutine()
    {
        Transform player = Player.Player.instance.transform;
        float duration = 0.5f;
        float stopDistance = 0.5f;
        float updateInterval = 0.05f;

        // 初始 tweener
        Tweener moveTween = transform.DOMove(player.position, duration)
            .SetEase(Ease.OutQuad)
            .SetAutoKill(false)
            .SetLoops(-1);

        while (true)
        {
            float distance = Vector3.Distance(transform.position, player.position);

            if (distance <= stopDistance)
            {
                moveTween.Kill();
                gameObject.transform.parent.gameObject.SetActive(false);
                yield break;
            }

            // 更新目标位置
            moveTween.ChangeEndValue(player.position, true);
            yield return new WaitForSeconds(updateInterval);
        }
    }


}






